/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bank.centraal;

import bank.bankieren.Money;
import bank.internettoegang.IBalie;
import bank.internettoegang.IRemoteListener;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

/**
 *
 * @author Jan
 */
public class Centraal  extends UnicastRemoteObject implements ICentraal
{

    ArrayList<IBalie> listeners = new ArrayList<IBalie>();

    public Centraal() throws RemoteException {
	/*	this.balie = balie;
		loginaccounts = new HashMap<String, ILoginAccount>();
		//sessions = new HashSet<IBankiersessie>();
		random = new Random();
                //loginaccounts.put("bart", new LoginAccount("bart", "b", 1));
                //loginaccounts.put("tobias", new LoginAccount("t", "t", 2));   */
    }

    public void addListener(IBalie listener) throws RemoteException {
        System.out.println("listener added: "+ listener);
        listeners.add(listener);
        System.out.println("num listeners: "+ listeners.size());
    }

    public void removeListener(IBalie listener) throws RemoteException {
        listeners.remove(listener);
    }

    public boolean maakRemoteOver(int destination, Money amount) throws RemoteException {
        String bank = "";
        String dest = ""+destination;
        String[] number = dest.split("");
        System.out.println("start maakRemoteOver");
        boolean ret = false;
        for (int i=0;i<dest.length(); i++)
        {
            if (dest.charAt(i) == "0".toCharArray()[0])
            {
                bank = dest.substring(0, i);
                break;
            }
        }
        System.out.println("to bank:" + bank);
        System.out.println("listeners.size():" + listeners.size());
        System.out.println("listeners " + listeners.toString());
        for (int i = 0;i < listeners.size(); i++)
        {
            
            String[] s = listeners.get(i).getBankString().split("");
            String b = listeners.get(i).getBankString();
            System.out.println("b="+b);
            System.out.println("if " + s[s.length-1] + " equals " + number[0]);
            if (s[s.length-1].equals(number[0]))
            {
                ret = listeners.get(i).makeRemoteOver(destination,amount);
                System.out.println("weirdass maak over");
            }
            System.out.println("if " + bank + " equals " + b);
            if (bank.equals(b))
            {
                 ret = listeners.get(i).makeRemoteOver(destination,amount);
                 System.out.println("mijn maak over");
            }
        }
        return ret;
    }
}
